package com.fei.socket;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class HandleMessage implements Runnable {

	private Socket socket;

	public HandleMessage(Socket socket) {
		this.socket = socket;

		new Thread(this).start();
	}

	@Override
	public void run() {
		try {
			// 读取客户端数据
			InputStream input = socket.getInputStream();
			OutputStream output = socket.getOutputStream();
			
			Request request = new Request(input);
			request.parse();

			// create Response object
			Response response = new Response(output);
			response.setRequest(request);
			response.sendStaticResource();

			output.close();
			input.close();
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("服务器 run 异常: " + e.getMessage());
		} finally {
			if (socket != null) {
				try {
					socket.close();
				} catch (Exception e) {
					socket = null;
					System.out.println("服务端 finally 异常:" + e.getMessage());
				}
			}
		}
	}

}